//
//  RegistTableViewCell.h
//  CommunityNannyO2O
//
//  Created by apple on 15/8/13.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "RegistTableViewCell.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MJExtension.h"
#import "AccountTool.h"
#import "UIViewController+Create.h"
#import "CustomTextField.h"
#import "AppConfig.h"
#import "RegistTableViewCell.h"
#import "QXNetWorkTool.h"

@interface RegistTableViewCell()<QXNetWorkToolDelegate>
{
    NSInteger timeCounter;
    NSTimer *timer;
}

@property (weak, nonatomic) IBOutlet CustomTextField  *phone;

@property (weak, nonatomic) IBOutlet CustomTextField  *verificationCode;

@property (weak, nonatomic) IBOutlet CustomTextField  *password;

@property (weak, nonatomic) IBOutlet CustomTextField  *passwordAgain;

@property (weak, nonatomic) IBOutlet UIButton *sendVerficationCode;

@property (weak, nonatomic) IBOutlet UIButton *regist;

@end

@implementation RegistTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"RegistTableViewCell";
    RegistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:ID owner:nil options:nil]firstObject];
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    return cell;
}

-(void)awakeFromNib
{
    //叉号，一次性删除输入框中的内容
    [self.phone setClearButtonMode:UITextFieldViewModeWhileEditing];
    [self.verificationCode setClearButtonMode:UITextFieldViewModeWhileEditing];
    [self.password setClearButtonMode:UITextFieldViewModeWhileEditing];
    [self.passwordAgain setClearButtonMode:UITextFieldViewModeWhileEditing];
    
    //数字键盘
    self.phone.keyboardType = UIKeyboardTypeNumberPad;
    self.verificationCode.keyboardType = UIKeyboardTypeNumberPad;
    
    //编辑时清空
    self.phone.clearsOnBeginEditing = YES;
    self.verificationCode.clearsOnBeginEditing = YES;
    self.password.clearsOnBeginEditing = YES;
    self.password.clearsOnBeginEditing = YES;
    
    //placeholder字体、颜色
    
    //    UIFont *font = [UIFont fontWithName:@"Hiragino Sans GB" size:12];
    //
    //    UIColor *color = UIColorRGBA(228, 231, 230, 1);
    
    //    UIPlaceHolderSelfDefineFontAndSize(@"Hiragino Sans GB", 12)
    //    [self.phone setFont:font
    //                  Color:color];
    
    //    [self.verificationCode setFont:UIPlaceHolderDefaultFont
    //                             Color:UIPlaceHolderDefaultColor];
    //
    //    [self.password setFont:UIPlaceHolderDefaultFont
    //                     Color:UIPlaceHolderDefaultColor];
    //
    //    [self.passwordAgain setFont:UIPlaceHolderDefaultFont
    //                          Color:UIPlaceHolderDefaultColor];
    
    //密码
    self.password.secureTextEntry = YES;
    self.passwordAgain.secureTextEntry = YES;
    
    
    //叉号
    UIButton *clearButton;
    clearButton = [self.phone valueForKey:@"_clearButton"];
    [clearButton setImage:[UIImage imageNamed:@"ico_login_del"]
                 forState:UIControlStateNormal];
    
    clearButton = [self.verificationCode valueForKey:@"_clearButton"];
    [clearButton setImage:[UIImage imageNamed:@"ico_login_del"]
                 forState:UIControlStateNormal];
    
    clearButton = [self.password valueForKey:@"_clearButton"];
    [clearButton setImage:[UIImage imageNamed:@"ico_login_del"]
                 forState:UIControlStateNormal];
    
    clearButton = [self.passwordAgain valueForKey:@"_clearButton"];
    [clearButton setImage:[UIImage imageNamed:@"ico_login_del"]
                 forState:UIControlStateNormal];
    
     [self.sendVerficationCode addTarget:self action:@selector(verficationCodeSender:) forControlEvents:UIControlEventTouchUpInside];
    
    self.regist.layer.cornerRadius = 16;
    self.regist.layer.masksToBounds = YES;
    [self.regist.layer setBorderWidth:1.0]; //边框宽度
    [self.regist.layer setBorderColor:UIColorRGBA(255, 255, 255, 1).CGColor];//边框颜色
    [self.regist addTarget:self action:@selector(registSender:) forControlEvents:UIControlEventTouchUpInside];
    
    //手势
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    
    [self addGestureRecognizer:tapGestureRecognizer];
    
    //子view
    UIView *containView1 = [self viewWithTag: 1];
    
    UITapGestureRecognizer *tapGestureRecognizer1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containView1 addGestureRecognizer:tapGestureRecognizer1];
    
    //子view
    UIView *containView2 = [self viewWithTag: 2];
    
    UITapGestureRecognizer *tapGestureRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containView2 addGestureRecognizer:tapGestureRecognizer2];
    
    //
    UIView *containView3 = [self viewWithTag: 3];
    
    UITapGestureRecognizer *tapGestureRecognizer3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containView3 addGestureRecognizer:tapGestureRecognizer3];
    
    UIView *containView4 = [self viewWithTag: 4];
    
    UITapGestureRecognizer *tapGestureRecognizer4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containView4 addGestureRecognizer:tapGestureRecognizer4];
}

-(void)tap
{
    [self.phone resignFirstResponder];
    [self.verificationCode resignFirstResponder];
    [self.password resignFirstResponder];
    [self.passwordAgain resignFirstResponder];
}

- (BOOL)isCorrectPhone
{
    if (self.phone.text.length != 11)
    {
        [MBProgressHUD showError:@"请输入正确的11位手机号"];
        
        return NO;
    }
    
    return YES;
}

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    
    switch (tag)
    {
        case 1:
            
            if (Response == nil) {
                
                [MBProgressHUD showError:@"获取验证码失败"];
                
                return;
            }
            
            [MBProgressHUD showSuccess:@"获取验证码成功"];
            
            timeCounter = 61;//60秒倒计时
            
            timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];

            
            
        case 2:
            for (NSDictionary *dic in Response)
            {
                
            }
    }
}

-(void)timeFireMethod
{
    if (timeCounter < 0)
    {
        return;
    }
    
    timeCounter--;
    
    [self.sendVerficationCode setTitleColor:UIColorRGBA(230, 69, 68, 1) forState:UIControlStateNormal];
    
    self.sendVerficationCode.titleLabel.font =
    [UIFont fontWithName:DefaultFont size:12];
    
    [self.sendVerficationCode setTitle:[NSString stringWithFormat:@"重新发送(%ld)",(long)timeCounter] forState:UIControlStateNormal];
    
    self.sendVerficationCode.userInteractionEnabled = NO;
    
    if(timeCounter <= 0)
    {
        [timer invalidate];
        timer = nil;
        [self.sendVerficationCode setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.sendVerficationCode.userInteractionEnabled = YES;
    }
}


- (void)registSender:(id)sender
{
    if ([self isCorrectPhone] == NO)
    {
        return;
    }
    
    QXNetWorkTool *QXnetWorkTool = [[QXNetWorkTool alloc]init];
    
    QXnetWorkTool.QXNetWorkDelegate = self;
    
    //    [QXnetWorkTool GetDataFromServerWithPostMethodandInterfaceName:LoginInterface andParameter:@[@{@"phone":_phone.text}] tag:2];
}

- (void)verficationCodeSender:(id)sender
{
    
    if ([self isCorrectPhone] == NO)
    {
        return;
    }
    
    QXNetWorkTool *QXnetWorkTool = [[QXNetWorkTool alloc]init];
    
    QXnetWorkTool.QXNetWorkDelegate = self;
//    
//    [QXnetWorkTool GetDataFromServerWithPostMethodandInterfaceName:VerificationCodeInterface andParameter:@[@{@"phone":_phone.text}] tag:1];
    
}

@end




















